<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-8-31
 * Func   天津时时彩
 */
defined('IN_YuLin') || exit('NO PERMIT!');

echo '<pre>';
// 获取各类时时彩的的后台设置信息
$lottCateTab  = Table('lottery_category');
$lottTab      = Table('lottery');
$field        = 'cate.name as cate_name, cate.tags as cate_tags, lott.sname as lott_sname, lott.title as lott_name, lott.id as lott_id';
$sql          = 'SELECT ' . $field . ' FROM ' . $lottCateTab . ' AS cate
                LEFT JOIN ' . $lottTab . ' AS lott
                ON cate.`id` = lott.`cate_id`
                WHERE cate.`sname` = "ssc"';
$sscList = $db->getall($sql);
if (!$sscList) {
    echo '彩种信息获取失败！'; 
    return;
}
// 设置彩种补全信息
foreach ($sscList as $key => $value){
    switch ($value['lott_sname']){
        case 'cqssc':
            $sscList[$key]['total_by_day'] = '120';
            $sscList[$key]['begin_index']  = '24';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '09:50-21:50', 'secont' => 10 * 60, 's_total' => 72),
                '1' => array('slot'   => '21:50-01:45', 'secont' => 5 * 60, 's_total' => 48)
            );
            break;
        case 'xjssc':
            $sscList[$key]['total_by_day'] = '96';
            $sscList[$key]['begin_index']  = '1';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '09:58-01:58', 'secont' => 10 * 60, 's_total' => 96),
            );
            break;
        case 'tjssc':
            $sscList[$key]['total_by_day'] = '84';
            $sscList[$key]['begin_index']  = '1';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '08:55-22:55', 'secont' => 10 * 60, 's_total' => 84),
            );
            break;
        case 'dfssc':
            $sscList[$key]['total_by_day'] = '120';
            $sscList[$key]['begin_index']  = '1';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '09:00-23:00', 'secont' => 10 * 60, 's_total' => 1440),
            );
            break;
        default:
            break;
    }
}
$preTab      = Table('lottery_k3');
// 准备进行当天期号生成
$baseDayTime = date('ymd');
foreach ($sscList as $k => $v){
    $checkCountSql  = 'SELECT COUNT(id) as cnt FROM ' . $preTab . ' WHERE tid = ' . $v['lott_id'] . ' AND `day` = "' . $baseDayTime . '"';
    echo $checkCountSql . ' <br/>';
    $checkCountInfo = $db->getrow($checkCountSql);
    if ($checkCountInfo['cnt'] == $v['total_by_day']) continue;
    $index          = 0;
    $begin_index    = $v['begin_index'];
    foreach ($v['time_slot'] as $tk => $tv){
        // 获取期号开始时间与结束时间戳
        $slotData  = explode('-', $tv['slot']);
        $btime     = strtotime(date('Y-m-d') . $slotData[0]);
        $etime     = '';
        if ($slotData[1] < $slotData[0]){
            // 第二天
            $etime = strtotime(date('Y-m-d', strtotime('+1 day')) . $slotData[1]);
        }else{
            $etime = strtotime(date('Y-m-d') . $slotData[1]);
        }
        while ($etime >= $btime){
            if($index >= $v['total_by_day'] && $tk == (count($v['time_slot']) - 1)) break;
            $index++;
            if ($begin_index > $v['total_by_day']) $begin_index = 1;
            $qihao  = $baseDayTime . str_pad( ($begin_index),3,0,STR_PAD_LEFT);
            $btime += $tv['secont'];
            // 基础期号累加
            $begin_index++;
            
            $ins = [];
            $ins['tid']		   = $v['lott_id'];
            $ins['period']	   = $qihao;
            $ins['starttime']  = $btime - $tv['secont'];
            $ins['stoptime']   = $btime;
            $ins['createtime'] = time();
            $ins['day']		   = $baseDayTime;
            $db->add('lottery_k3',$ins);
            print_r($ins);
        }
    }
}

exit('success');






















